package http_proxy_router

import (
	"github.com/gin-gonic/gin"
	admin_controller "github.com/youkelike/gateway/admin/controller"
	"github.com/youkelike/gateway/http_proxy_middleware"
)

func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {
	//todo 优化点1
	//router := gin.Default()
	router := gin.New()

	// pprof.Register(router)

	router.Use(middlewares...)
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	router.Static("/dist", "./dist")

	oauth := router.Group("/oauth")
	// oauth.Use(admin_middleware.TranslationMiddleware())
	{
		// 根据 appId 和 secret 来生成 jwt token，供给租户使用
		admin_controller.OAuthRegister(oauth)
	}

	router.Use(
		// 根据数据库中指定的匹配模式（域名匹配或前缀匹配），找到合适的配置参数
		http_proxy_middleware.HTTPAccessModeMiddleware(),

		// 用自定义的流量统计功能
		http_proxy_middleware.HTTPFlowCountMiddleware(),
		// 用 rate 包做的限流
		http_proxy_middleware.HTTPFlowLimitMiddleware(),

		http_proxy_middleware.HTTPWhiteListMiddleware(),
		http_proxy_middleware.HTTPBlackListMiddleware(),

		// 熔断降级
		// http_proxy_middleware.HTTPCircuitMiddleware(),

		// 租户使用 jwt
		http_proxy_middleware.HTTPJwtAuthTokenMiddleware(),
		// 用自定义的流量统计功能
		http_proxy_middleware.HTTPJwtFlowCountMiddleware(),
		// 用 rate 包做的限流
		http_proxy_middleware.HTTPJwtFlowLimitMiddleware(),

		http_proxy_middleware.HTTPHeaderTransferMiddleware(),
		http_proxy_middleware.HTTPStripUriMiddleware(),
		http_proxy_middleware.HTTPUrlRewriteMiddleware(),

		http_proxy_middleware.HTTPReverseProxyMiddleware())

	return router
}
